package com.wjw.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wjw.pojo.Book;
import com.wjw.service.BookSersive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("book")
public class BookController {
    @Value("${location}")
    private String location;
    //图书管理以及模糊查询
    @Autowired
    private BookSersive bookSersive;
    @RequestMapping("listBook")
    public String listBook(@RequestParam(required = false,value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(required = false,value = "pageSize",defaultValue = "10")Integer pageSize, Model model, Book book){
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Book>qw = new QueryWrapper<>();
        if (book.getBname() != null){
            qw.like("bname",book.getBname());
        }
        List<Book> list = bookSersive.list(qw);
        PageInfo<Book> objectPageInfo = new PageInfo<Book>(list);
        model.addAttribute("pageInfo", objectPageInfo);
        return "bookList";

    }
    @RequestMapping("preSaveBook")
    public String preSaveBook(){
        return "bookSave";
    }

    //添加书籍
    @RequestMapping("saveBook")
    public String saveBook(Book book, MultipartFile file){
        transFile(book,file);
        boolean save = bookSersive.save(book);
        return "redirect:/book/listBook";
    }

    private void transFile(Book book, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String prefix =System.nanoTime()+"";
        String path=prefix+suffix;
        File file1 = new File(location);
        if (!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(file1,path);
        try {
            file.transferTo(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        book.setBimage(path);

    }
    //删除图书
    @RequestMapping("delbook/{id}")
    public String delBook(@PathVariable("id") Integer id){
        boolean b = bookSersive.removeById(id);
        return "redirect:/book/listBook";
    }

    //获取图书信息进行回显,准备修改图书信息
    @RequestMapping("preUpdateBook/{id}")
    public String preUpdateBook(@PathVariable("id")Integer id,Model model){
        Book byId = bookSersive.getById(id);
        model.addAttribute("book",byId);
        return "bookUpdate";
    }

    //修改书籍
    @RequestMapping("updateBook")
    public String updateBook(Book book){
        boolean save = bookSersive.updateById(book);
        return "redirect:/book/listBook";
    }

    //批量删除图书
    @PostMapping("batchDeleteBook")
    @ResponseBody
    public String batchDeleteBook(String idList){
        String[] split= StrUtil.split(idList,",");
        List<Integer>list=new ArrayList<>();

        for (String s : split) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = bookSersive.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }

}
